﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using MSAcademic.v2.Infrastructure.Article;
using MSAcademic.v2.Infrastructure.SubSection;
using MSAcademic.v2.Infrastructure.User;

namespace MSAcademic.v2.Web.Models.Home
{
    
    public partial class IndexModel
    {
        public List<ISubSection> SubSections
        {
            get
            {
                if (_subSections == null)
                {
                    _subSections = new Lazy<List<ISubSection>>(() => _subSectionQueries.GetByName("Izdvojeno").ToList());
                }
                return _subSections.Value;
            }
        }
        private Lazy<List<ISubSection>> _subSections;

        public List<Tuple<List<Tuple<IArticle,IUser>>, ISubSection>> ArticlesWithSubSections
        {
            get
            {
                if (_articlesWithSubSections == null)
                {
                    _articlesWithSubSections = new Lazy< List<Tuple<List<Tuple<IArticle,IUser>>, ISubSection>>>(() => 
                        (from subSection in SubSections 
                         let articles = _articleQueries.GetArticlesForSubSection(subSection.Id).ToList()
                         select new Tuple<List<Tuple<IArticle,IUser>>, ISubSection>(
                             (from a in articles
                              let author=_userQueries.GetArticleAuthor(a.Id)
                              select new Tuple<IArticle,IUser>(a,author)).ToList(),subSection)).ToList());
                }
                return _articlesWithSubSections.Value;
            }
        }
        private Lazy<List<Tuple<List<Tuple<IArticle, IUser>>, ISubSection>>> _articlesWithSubSections;

        public List<IArticle> FeaturedArticles
        {
            get
            {
                if (_featuredArticles == null)
                {
                    _featuredArticles = new Lazy<List<IArticle>>(() => _articleQueries.GetArticlesForSubSectionName("Izdvojeno", false).ToList());
                }
                return _featuredArticles.Value;
            }
        }
        private Lazy<List<IArticle>> _featuredArticles;

        public List<Tuple<IArticle, IUser>> FeaturedArticlesWithAuthors
        {
            get
            {
                if (_featuredArticlesWithAuthors == null)
                {
                    _featuredArticlesWithAuthors = new Lazy<List<Tuple<IArticle, IUser>>>(() =>  
                        (from a in FeaturedArticles
                         let author = _userQueries.GetArticleAuthor(a.Id)
                         select new Tuple<IArticle,IUser>(a, author)).ToList());
                }
                return _featuredArticlesWithAuthors.Value;
            }
        }
        private Lazy<List<Tuple<IArticle, IUser>>> _featuredArticlesWithAuthors;

        public List<Tuple<IArticle,IUser>> LatestArticles
        {
            get
            {
                if (_latestArticles == null)
                {
                    _latestArticles = new Lazy<List<Tuple<IArticle, IUser>>>(() =>
                                                                             (from m in _articleQueries.GetLatestArticles(
                                                                              Convert.ToInt32(ConfigurationManager.AppSettings["LatestArticleIndexCount"])).ToList()
                                                                              let author = _userQueries.GetArticleAuthor(m.Id)
                                                                              select new Tuple<IArticle, IUser>(m, author)).ToList());             
              }
                return _latestArticles.Value;
            }
        }
        private Lazy<List<Tuple<IArticle, IUser>>> _latestArticles;
    }

    public partial class IndexModel
    {
        private readonly IArticleQueries _articleQueries;
        private readonly ISubSectionQueries _subSectionQueries;
        private readonly IUserQueries _userQueries;

        public IndexModel(IArticleQueries articleQueries,
                          ISubSectionQueries subSectionQueries,
                            IUserQueries userQueries)
        {
            _articleQueries = articleQueries;
            _subSectionQueries = subSectionQueries;
            _userQueries = userQueries;
        }

        public IndexModel()
        {

        }
    }
}